
import axios from 'axios'

// 创建axios实例
const service = axios.create({
    baseURL: 'http://127.0.0.1:8089', // 后端地址
    timeout: 5000, // 超时时间
});

// 请求拦截器
service.interceptors.request.use(
    config => {
        if (config.method === 'get') {
            config.params = {
                ...config.params,
                _t: Date.now(), // 防止GET缓存
            };
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        return response.data; // 返回响应数据
    },
    error => {
        return Promise.reject(error);
    }
);

// 获取用户列表
export const getUsers = async (params) => {
    try {
        const response = await service.get('/user/user', { params });
        if (response.code === 0) {
            return response;
        } else {
            throw new Error(response.message || '获取用户失败');
        }
    } catch (error) {
        console.error('获取用户失败:', error);
        throw error;
    }
};
